#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "Dialog.h"

#include "Class.h"
#include "Error.h"
#include "CmdNo.h"
#include "Window.h"
#include "Buttons.h"
#include "BorderItems.h"
#include "TextItem.h"
#include "Box.h"
#include "Separator.h"

//---- Dialog ------------------------------------------------------------------

NewMetaImpl(Dialog,Manager, (T(actionId)));

Dialog::Dialog(const char *nm, WindowFlags f)
								: Manager(nm, eMgrHide, eWinDialog)
{
	wflags= f;
	actionId= cIdNone;
}

Point Dialog::GetInitialWindowSize()
{
	return cAutoSize;
}

VObject *Dialog::DoMakeContent()
{
	return
		new VExpander(gPoint0,
			new Matte(gPoint10, DoMakeControls()),
			new Separator,
			new Matte(gPoint10, DoMakeActions()),
			0
		);
}

VObject *Dialog::DoMakeActions()
{
	return new TextItem("DoMakeActions");
}

VObject *Dialog::DoMakeControls()
{
	return new TextItem("DoMakeControls");
}

MenuBar *Dialog::DoMakeMenuBar()
{
	return 0;
}

void Dialog::EnableItem(int id, bool b)
{
	VObject *vop;
	if (vop= FindItem(id))
		vop->Enable(b, TRUE);
}

VObject *Dialog::FindItem(int id)
{
	Window *bw= GetWindow();
	if (bw) {
		VObject *vop= bw->GetViewedVObject();
		if (vop)
			return vop->FindItem(id);
	}
	return 0;
}

void Dialog::DoSetDefaults()
{
}

void Dialog::DoSave()
{
}

void Dialog::DoRestore()
{
}

void Dialog::DoStore()
{
}

void Dialog::DoSetup()
{
}

// called after window and its contents have been created
// but before window is mapped
void Dialog::AboutToOpen()
{
	DoSetDefaults();
	DoSave();
	DoSetup();
}

int Dialog::ShowAt(VObject *fp, Point p, bool resize, bool block)
{
	OpenAt(fp, p, resize, block);
	return actionId;
}

Point Dialog::GetInitialPos(Point e)
{
	VObject *dfltButton= GetDefaultButton();
	if (dfltButton)
		return dfltButton->GetPortPoint(dfltButton->contentRect.Center());
	return e.Half();
}

void Dialog::Cancel()
{
	actionId= cIdCancel;
	Dismiss();
	DoRestore();
}

bool Dialog::AboutToDismiss()
{
	if (GetFirstHandler()) {
		bool rc= GetFirstHandler()->KbdFocus(FALSE);
		if (!rc)
			return FALSE;
		GetFirstHandler()->KbdFocus(TRUE);
	}
	return TRUE;
}

void Dialog::Control(int id, int part, void *vp)
{
	if (id != cIdNone && part == cPartAction) {
		actionId= id;
		switch (id) {
		case cIdDefault:
			DoSetDefaults();
			break;

		case cIdYes:
		case cIdOk:
			if (Dismiss()) {
				DoStore();
				return;
			}
			break;

		case cCLOSE:
		case cIdCancel:
			actionId= cIdCancel;
			Cancel();
			return;

		case cIdNo:
			if (Close())
				return;
			break;
		
		case cHELP:
			break;
		
		default:
			if (Dismiss()) {
				DoStore();
				return;
			}
			break;
		}
	}
	Manager::Control(id, part, vp);
	DoSetup();
}

